<template>
    <div class="stock-identification-page">
        <el-form :inline="true" :model="identificationForm" class="demo-form-inline" style="border-bottom:1px solid #ccc;">
            <el-form-item label="库房标识编码" class="selectMB10">
                <el-input size="mini" v-model="identificationForm.code" placeholder="请输入库房标识编码"></el-input>
            </el-form-item>
            <el-form-item label="库房标识名称" class="selectMB10">
                <el-input size="mini" v-model="identificationForm.name" placeholder="请输入库房标识名称"></el-input>
            </el-form-item>
            <el-form-item label="库房标识描述" class="selectMB10">
                <el-input size="mini" v-model="identificationForm.remark" placeholder="请输入库房标识描述"></el-input>
            </el-form-item>
             <el-form-item label="当前状态" class="selectMB10" style="margin-right: 10px;">
                <el-select size="mini" clearable v-model="identificationForm.status" placeholder="请选择">
                    <el-option v-for="(status,index) in statusLists" :key="index" :label="status.label" :value="status.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item class="selectMB10">
                <el-button class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" size="mini" type="danger" @click="queryStockIdentification()">查询</el-button>
            </el-form-item>
        </el-form>
        <!--表格-->
        <label class="default-label">库房标识明细列表：</label>
        <!--新增按钮-->
        <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="createNew()">新增</el-button>
        <el-table :data="identificationData" border style="width: 100%;" v-loading.body="loading" element-loading-text="拼命加载中">
            <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>
            <el-table-column label="操作" header-align="center" align="center" width="100">
                <template scope="scope">
                    <div class="operationItem bolder" v-if="scope.row.status" @click="handleStatus(scope.$index,scope.row)">禁用</div>
                    <div class="operationItem bolder" v-else @click="handleStatus(scope.$index,scope.row)">启用</div> 
                    <div class="operationItem disabled bolder" v-if="scope.row.status">修改</div>
                    <div class="operationItem bolder" v-else @click="updateCorFact(scope.$index,scope.row)">修改</div>
                </template>
            </el-table-column>
            <el-table-column prop="statusName" label="当前状态" header-align="center" align="center" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column prop="code" label="库房标识编码" header-align="center" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column prop="name" label="库房标识名称" header-align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="remark" label="库房标识描述" header-align="center" show-overflow-tooltip></el-table-column>
        </el-table>
        <!--分页-->
        <el-pagination v-if="isPaging" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"  :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :page-count="totalPage" :total="totalNumber"></el-pagination>
         <!--新建弹窗-->
        <el-dialog title="新增库房标识" :visible.sync="dialogForm1Visible" size="tiny" :lock-scroll="false">
            <el-form :inline="true" :model="addStockIdentificationForm" class="demo-form-inline" style="border-bottom:1px solid #ccc;" ref="addStockIdentificationForm"> 
                <el-form-item label="库房标识编码" prop="code"  :rules="[{ required: true, message: '库房标识编码不能为空'},{ max: 20, message: '最多不可超过20字符'}]" :label-width="labelWidth">
                    <el-input  v-model.trim="addStockIdentificationForm.code" placeholder="请输入库房标识编码"></el-input>
                </el-form-item>
                <el-form-item label="库房标识名称" prop="name"  :rules="[{ required: true, message: '库房标识名称不能为空'},{ max: 200, message: '最多不可超过200字符'}]" :label-width="labelWidth">
                    <el-input  v-model.trim="addStockIdentificationForm.name" placeholder="请输入库房标识名称"></el-input>
                </el-form-item>
                <el-form-item label="库房标识描述" prop="remark"  :rules="[{ max: 200, message: '最多不可超过200字符'}]" :label-width="labelWidth">
                    <el-input  v-model.trim="addStockIdentificationForm.remark" placeholder="请输入库房标识描述"></el-input>
                </el-form-item>
                <el-form-item label="状态" :label-width="labelWidth">
                    <el-radio-group v-model="addStockIdentificationForm.status">
                        <el-radio :label="true">启用</el-radio>
                        <el-radio :label="false">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="saveStockIdentification">保存</el-button>
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="cancelStockIdentification">取消</el-button>
            </div>
        </el-dialog>
        <!--修改弹窗-->
        <el-dialog title="修改库房库房标识" :visible.sync="dialogForm2Visible" size="tiny" :lock-scroll="false">
            <el-form :inline="true" :model="updateStockIdentificationForm" class="demo-form-inline" style="border-bottom:1px solid #ccc;" ref="updateStockIdentificationForm">
                <el-form-item label="库房标识编码" :label-width="labelWidth">
                    <el-input  v-model.trim="updateStockIdentificationForm.code" disabled></el-input>
                </el-form-item>
                <el-form-item label="库房标识名称" prop="name"  :rules="[{ required: true, message: '库房标识名称不能为空'},{ max: 200, message: '最多不可超过200字符'}]" :label-width="labelWidth">
                    <el-input  v-model.trim="updateStockIdentificationForm.name" :title="updateStockIdentificationForm.name"></el-input>
                </el-form-item>
                <el-form-item label="库房标识描述" prop="remark"  :rules="[{ max: 200, message: '最多不可超过200字符'}]" :label-width="labelWidth">
                    <el-input  v-model.trim="updateStockIdentificationForm.remark" placeholder="请输入库房标识描述"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="saveUpdateStockIdentification">保存</el-button>
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="cancelUpdateStockIdentification">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import Vue from 'vue'
    export default {
        data() {
            return {
                // 查询表单
                identificationForm:{
                    name:"",
                    code:"",
                    remark:"",
                    status:""
                },
                statusLists:[],   //当前状态下拉列表
                labelWidth:"100px",
                addStockIdentificationForm:{
                    name:"",
                    code:"",
                    remark:"",
                    status:false
                },
                updateStockIdentificationForm:{
                    name:"",
                    code:"",
                    remark:""
                },
                identificationData:[],
                loading:false,
                disabled:false,


                //弹窗
                dialogForm1Visible:false,
                dialogForm2Visible:false,
                // 分页
                isPaging:false,
                pageIndex:1,    //当前页（必传）
                pageSize :10,   //每页多少条
                totalNumber: 20,
                totalPage:2,
                
                //查询禁用
                selectDisabled:false
            };
        },
        //进入页面立即运行此方法
        mounted: function () {
                var _this = this;
                _this.$store.commit('currentActive', 'StockIdentification');
                // //ajax前后查询按钮禁用
                // $(".selectAll").ajaxStart(function(){
                //     _this.selectDisabled=true;
                // });
                // $(".selectAll").ajaxStop(function(){
                //     _this.selectDisabled=false;
                // });
                _this.dropDownValue();// 当前状态下拉列表
                _this.queryStockIdentification();    //表单查询
        },
        methods:{
            // 当前状态下来列表
            dropDownValue(){
                var _this=this;
                var tmp=["warehouseTag.warehouseTagStatus"];
                $.ajax({
                    type:'POST',
                    url:_this.iwm_service+'iwm/getPullDownList',
                    cache:false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType:"application/json;charset=utf-8",
                    success:function(data){
                        if(data.success){
                            _this.statusLists=data.data['warehouseTag.warehouseTagStatus'];
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //查询库房标识列表
            queryStockIdentification(){
                var _this=this;
                _this.loading=true;
                var tmp={
                    name:_this.identificationForm.name,
                    code:_this.identificationForm.code,
                    remark:_this.identificationForm.remark,
                    status:_this.identificationForm.status,
                    pageIndex:_this.pageIndex,
                    pageSize:_this.pageSize
                };
                _this.selectDisabled=true;
                $.ajax({
                    type: 'POST',
                    url: _this.iwm_service+"warehouseTag/queryPage",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            _this.loading=false;
                            _this.isPaging=true;
                            _this.totalPage=data.data.totalPage;
                            _this.totalNumber=data.data.totalNumber;
                            _this.identificationData=data.data.dataList;
                        }else{
                            _this.common.errorTip(data.errorMessage)
                        }
                        _this.selectDisabled=false;
                    }
                });
            },
            //新增库房标识信息
            createNew(){
                var _this=this;
                _this.dialogForm1Visible=true; 
                if(this.$refs['addStockIdentificationForm']) {
                    this.$refs['addStockIdentificationForm'].resetFields();
                }
                _this.addStockIdentificationForm.name="";
                _this.addStockIdentificationForm.code="";
                _this.addStockIdentificationForm.status=false;
                _this.addStockIdentificationForm.remark="";
            },
            //新建保存
            saveStockIdentification(){
                var _this=this;
                _this.$refs["addStockIdentificationForm"].validate((valid) => {
                    if (valid) {
                        var tmp={
                            name:_this.addStockIdentificationForm.name,   //去除税码两边的空格
                            code:_this.addStockIdentificationForm.code,
                            status:_this.addStockIdentificationForm.status,
                            remark:_this.addStockIdentificationForm.remark
                        };
                        $.ajax({
                            type: 'POST',
                            url: _this.iwm_service+"warehouseTag/add",
                            cache: false,
                            data:JSON.stringify(tmp),
                            dataType:'JSON',
                            contentType: "application/json;charset=utf-8",
                            success: function (data) {
                                if (data.success) {
                                    _this.common.successTip("保存成功！");
                                    _this.dialogForm1Visible=false;
                                    _this.queryStockIdentification();
                                }else{
                                    _this.common.errorTip(data.errorMessage);
                                }
                            }
                        });
                    } else {
                        _this.common.errorTip("存在未填写的必填项！");
                        return false;
                    }
                });
            },
            //新建取消
            cancelStockIdentification(){
                var _this=this;
                _this.dialogForm1Visible=false;
                if(this.$refs['addStockIdentificationForm']) {
                    this.$refs['addStockIdentificationForm'].resetFields();
                }
                _this.addStockIdentificationForm.name="";
                _this.addStockIdentificationForm.code="";
                _this.addStockIdentificationForm.remark="";
                _this.addStockIdentificationForm.status=false;
            },
            // 操作启用禁用
            handleStatus(index, row){
                var _this=this;
                if(row.status){
                    row.status = false;
                }else{
                    row.status = true;
                }
                var tmp={id: row.id,status: row.status}; 
                $.ajax({
                    type: 'POST',
                    url: _this.iwm_service+"warehouseTag/updateStatus",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            // 当前状态的显示
                            if(row.status){
                                row.statusName = "启用";
                                _this.common.successTip("启用成功！");
                            }else{
                                row.statusName = "禁用";
                                _this.common.successTip("禁用成功！");
                            }
                        }else
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            //点击修改
            updateCorFact(index,row){
                var _this=this;
                _this.dialogForm2Visible=true;
                if(this.$refs['updateStockIdentificationForm']) {
                    this.$refs['updateStockIdentificationForm'].resetFields();
                 }
                 _this.updateStockIdentificationForm = Vue.util.extend(_this.updateStockIdentificationForm, row);
                //  _this.updateStockIdentificationForm.id = row.id;
                //  _this.updateStockIdentificationForm.name = row.name;
                //  _this.updateStockIdentificationForm.code = row.code;
                //  _this.updateStockIdentificationForm.remark = row.remark;
            },
            //修改保存
            saveUpdateStockIdentification(){
                var _this=this;
                _this.$refs["updateStockIdentificationForm"].validate((valid) => {
                    if (valid) {
                        var tmp={
                            id:_this.updateStockIdentificationForm.id,
                            name:_this.updateStockIdentificationForm.name,
                            code:_this.updateStockIdentificationForm.code,
                            remark:_this.updateStockIdentificationForm.remark
                            };
                        $.ajax({
                            type: 'POST',
                            url: _this.iwm_service+"warehouseTag/update",
                            cache: false,
                            data:JSON.stringify(tmp),
                            dataType:'JSON',
                            contentType: "application/json;charset=utf-8",
                            success: function (data) {
                                if (data.success) {
                                    _this.common.successTip("修改成功！");
                                    _this.dialogForm2Visible=false;
                                    _this.queryStockIdentification();
                                }else
                                    _this.common.errorTip(data.errorMessage);
                            }
                        });
                    } else {
                        _this.common.errorTip("存在未填写的必填项！");
                        return false;
                    }
                });
            },
            //修改取消
            cancelUpdateStockIdentification(){
                var _this=this;
                _this.dialogForm2Visible=false;
            },
            //   分页
            handleSizeChange(val) {
                var _this=this;
                _this.pageSize=val;
                _this.queryStockIdentification();

            },
            //点击页码分页查询点击事件
            handleCurrentChange(val) {
                var _this=this;
                _this.pageIndex=val;
                _this.queryStockIdentification();
            },
            // // ===================================
            // successTip(p) {     //成功时的提示信息
            //     this.$notify({
            //         title: '成功',
            //         message: p,
            //         type: 'success'
            //     });
            // },
            // errorTip(p) {     //错误提示
            //     this.$notify.error({
            //         title: '错误',
            //         message: p
            //     });
            // }

        }
    }
</script>
